package com.jsu.config;

import org.example.model.pay.enums.PayStatus;
import com.jsu.mapper.PaymentMapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.example.model.pay.pojo.Payment;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 消息消费者
 */
@Slf4j
@Component
public class QueueConsumer {

    @Autowired
    private PaymentMapper paymentMapper;

    // 监听延迟队列A
    @RabbitListener(queues = RabbitMqConfig.DELAY_QUEUE_NAME)
    public void receiveA(Integer id,Message message1, Channel channel) throws IOException {
        Integer paymentId = Integer.valueOf(id);
        log.info("当前时间: {},  延迟队列收到订单编号: {}", LocalDateTime.now(), paymentId);

        // 根据订单编号查询订单
        Payment payment = paymentMapper.selectById(paymentId);
        log.info(payment.toString());
        //还未付款，则直接取消订单
        if(payment.getStatus() == PayStatus.WAIT_BUYER_PAY.getValue()){
            payment.setStatus(PayStatus.TRADE_CLOSED.getValue());
            payment.setUpdateTime(new Date());
            paymentMapper.updateById(payment);
        }
        //手动签收
        channel.basicAck(message1.getMessageProperties().getDeliveryTag(),false);
    }

}
